package com.tanhua.server.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Value("${tanhua.sso.url}")
    private String url;

    public User queryUserByToken(String token) {
        String template = restTemplate.getForObject(url + "/user/{token}", String.class, token);
        if (template != null) {
            try {
                return MAPPER.readValue(template, User.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}
